package net.xdclass.controller;


import net.xdclass.controller.request.AccountLoginRequest;
import net.xdclass.controller.request.AccountRegisterRequest;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.service.AccountService;
import net.xdclass.service.FileService;
import net.xdclass.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 二当家小D
 * @since 2025-02-20
 */
@RestController
@RequestMapping("/api/account/v1")
public class AccountController {
    
    @Autowired
    private FileService fileService;

    @Autowired
    private AccountService accountService;

    /**
     * 文件上传
     * @RequestPart:接收文件以及更为复杂的数据类型
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public JsonData uploadUserImg(@RequestPart("file") MultipartFile file){

        String url = fileService.uploadUserImg(file);
        return url != null ? JsonData.buildSuccess(url) : JsonData.buildResult(BizCodeEnum.FILE_UPLOAD_USER_IMG_FAIL);
    }

    /**
     * 用户注册
     * @param accountRegisterRequest
     * @return
     */
    @PostMapping("/register")
    public JsonData register(@RequestBody AccountRegisterRequest  accountRegisterRequest ) {

        JsonData  jsonData =  accountService.register(accountRegisterRequest);
        return jsonData;
    }


    /**
     * 用户登录
     * @param accountLoginRequest
     * @return
     */
    @PostMapping("/login")
    public JsonData login(@RequestBody AccountLoginRequest accountLoginRequest ) {
        JsonData  jsonData =  accountService.login(accountLoginRequest);
        return jsonData;
    }

}

